package com.example.inoculations.controller;

import com.example.inoculations.entity.Menu;
import com.example.inoculations.mapper.MenuMapper;
import com.example.inoculations.resultR.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meirui
 * @since 2022-11-19
 */
@Controller
@ResponseBody
@RequestMapping("/inoculations/menu")
public class MenuController {
    @Autowired
    MenuMapper menuMapper;

    /**
     * 查询所有菜单如果mid为空，则是一级菜单
     */
    @GetMapping("/menu")
    public R allMenu(){
        List<Menu> list = menuMapper.selectList(null);
        List<Menu> parNode = list.stream().filter(menu -> menu.getMid() == null).collect(Collectors.toList());
        for( Menu it : parNode){
            it.setChildren(list.stream().filter(m->it.getId().equals(m.getMid())).collect(Collectors.toList()));
        }
        return R.SUCCESS(parNode);
    }
}
